package com.arcaneshift.servlet.util.parser;

import java.io.InputStream;
import java.util.List;

import com.arcaneshift.model.response.StoryResponse;
import com.arcaneshift.servlet.abstracts.XMLParserBase;
import com.arcaneshift.servlet.util.game.ChangePhaseStory;
import com.arcaneshift.servlet.util.game.DrawCardStory;
import com.arcaneshift.servlet.util.game.Story;
import com.arcaneshift.servlet.util.network.HTTPProtocol;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractResponse;
import com.common.security.exceptions.HTTPException;
import com.common.security.exceptions.ParserException;

/**
 * The ActionXMLParser does not implement a parseRequest method as the game using
 * channel does not need one.
 * 
 * @author Ady
 *
 */
public class StoryXMLParser extends XMLParserBase {

	@Override
	public AbstractRequest parseRequest(InputStream stream) throws HTTPException, ParserException {
		return null;
	}

	@Override
	public String parseResponse(AbstractResponse abstractResponse) throws ParserException {
		StoryResponse response = null;
		if (abstractResponse instanceof StoryResponse) {
			response = (StoryResponse) abstractResponse;
		} else {
			throw new ParserException(ParserException.RESPONSE_MATCH_ERROR);
		}
		StringBuilder builder = new StringBuilder();
		List<Story> stories = response.getStories();
		
		builder.append(HTTPProtocol.RESPONSE_TAG);
		builder.append(HTTPProtocol.CLOSE_TAG);
		builder.append(HTTPProtocol.END_LINE);
		builder.append(getAction(response.getRequestAction()));
		builder.append(HTTPProtocol.END_LINE);
		builder.append(HTTPProtocol.CONTENT_TAG);
		builder.append(HTTPProtocol.END_LINE);
		for (Story story : stories) {
			builder.append(getStory(story));
			builder.append(HTTPProtocol.END_LINE);
		}
		builder.append(HTTPProtocol.CONTENT_END_TAG);
		builder.append(HTTPProtocol.END_LINE);
		builder.append(HTTPProtocol.RESPONSE_END_TAG);
		
		return builder.toString();
	}
	
	private String getStory(Story story) {
		StringBuilder builder = new StringBuilder();
		
		builder.append(HTTPProtocol.STORY_TAG);
		builder.append(HTTPProtocol.SPACE);
		builder.append(HTTPProtocol.VALUE_ATTRIBUTE);
		builder.append(story.getType());
		builder.append(HTTPProtocol.END_ATTRIBUTE);
		builder.append(HTTPProtocol.SPACE);
		builder.append(HTTPProtocol.PLAYER_ATTRIBUTE);
		builder.append(story.getPlayer());
		builder.append(HTTPProtocol.END_ATTRIBUTE);
		builder.append(HTTPProtocol.CLOSE_TAG);
		builder.append(HTTPProtocol.END_LINE);
		
		switch (story.getType()) {
			case DRAW_CARD: {
				builder.append(getDrawCardStory((DrawCardStory) story));
				break;
			}
			case CHANGE_PHASE: {
				builder.append(getChangePhaseStory((ChangePhaseStory) story));
				break;
			}
		}
		
		builder.append(HTTPProtocol.END_LINE);
		builder.append(HTTPProtocol.STORY_END_TAG);
		
		return builder.toString();
	}
	
	private String getDrawCardStory(DrawCardStory story) {
		return getCard(story.getCard());
	}
	
	private String getChangePhaseStory(ChangePhaseStory story) {
		return getPhase(story.getPhase());
	}
}
